<template>
  <div class="login">
    <div class="login-right">
      <div class="login-form">
        <a-card class="box-card">
          <div class="login-form__text">{{ title }}</div>
          <a-form
            ref="formRef"
            :model="formState"
            class="login-form__input"
            :rules="rules"
            :label-col="{ span: 4 }"
            :wrapper-col="{ span: 19 }"
          >
            <a-form-item ref="username" label="用户名" name="username">
              <a-input v-model:value="formState.username" />
            </a-form-item>
            <a-form-item ref="password" label="密码" name="password">
              <a-input-password v-model:value="formState.password" />
            </a-form-item>
          </a-form>
          <div class="login-form__button">
            <a-button type="primary" @click="handleSubmit" :loading="isLogin">
              登录
            </a-button>
          </div>
        </a-card>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
  import { computed, reactive, ref } from 'vue';
  import type { Rule } from 'ant-design-vue/es/form';
  import { useAuthApi } from '@/api/auth';
  import { useUserInfo } from '@/store/user';
  import pinia from '@/store';
  import { useRouter } from 'vue-router';
  import { setAddRoute } from '@/router';

  interface FormState {
    username: string;
    password: string;
    remember: boolean;
  }

  const rules: Record<string, Rule[]> = {
    username: [
      { required: true, message: '请输入用户名', trigger: 'change' },
      { min: 3, message: '最低长度 3', trigger: 'blur' },
    ],
    password: [
      { required: true, message: '请输入密码', trigger: 'change' },
      // { min: 3, max: 5, message: '长度应为 3 到 5', trigger: 'blur' },
    ],
  };

  const router = useRouter();
  const authApi = useAuthApi();
  const userInfo = useUserInfo(pinia);

  const formRef = ref();
  const isLogin = ref(false);

  const formState = reactive<FormState>({
    username: '',
    password: '',
    remember: true,
  });

  const title = computed(() => {
    return import.meta.env.VITE_APP_TITLE;
  });

  const handleSubmit = async () => {
    try {
      await formRef.value.validate();
      isLogin.value = true;

      const { data } = await authApi.login(formState);
      userInfo.setUserInfo(data.username, data.token);
      setAddRoute();
      isLogin.value = true;

      router.push({ path: '/' });
    } catch (error) {
      console.error('表单验证失败:', error);
    } finally {
      isLogin.value = false;
    }
  };
</script>

<style lang="scss" scoped>
  $font-color: #000; //声明变量

  .login {
    width: 100%;
    height: 100vh;
    overflow-y: hidden;
    display: flex;
    justify-content: space-around;
    background: url('../../assets/images/new_bg.jpg');
    background-repeat: no-repeat;
    background-size: 100%;
    font-family:
      Microsoft YaHei-Bold,
      Microsoft YaHei;
  }

  .login-right {
    width: 44%;
    height: 100%;
    display: flex;
    /*父元素设置flex属性*/
    justify-content: center;
    /*水平主轴居中*/
    align-items: center;
    /*垂直交叉轴居中*/
  }

  .ant-card {
    width: 520px;
    height: 380px;
    border-radius: 10px;
    text-align: center;

    .login-form__text {
      font-size: 36px;
      font-weight: bold;
      line-height: 86px;
      color: rgb(0, 26, 147);
    }

    :deep(.ant-form-item-has-error .ant-form-item-explain) {
      text-align: left !important;
    }
  }

  .box-card {
    box-shadow: 5px 5px 10px #4b4a4a;

    .login-form__input {
      padding: 12px 30px;
      margin-top: 20px;
    }

    .login-form__button {
      padding: 12px;
    }

    .ant-btn {
      width: 200px;
      height: 51px;
      font-size: 16px;
      border-radius: 12px;
      background-color: #3c6cfe;
    }
  }
</style>
